package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.AccountConfigExt;
import com.tbit.uqbike.object.pojo.MoveConfig;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.AccountConfigExtService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import java.util.List;
import java.util.Map;


/**
 * 区域参数扩展配置Controller
 *
 * @author jing
 * @date 2021-04-21
 */
@RestController
@RequestMapping("/accountconfigext")
public class AccountConfigExtController extends BaseController {
    @Autowired
    private AccountConfigExtService accountConfigExtService;

    /**
     * 查询区域参数扩展配置列表,当参数不存在时，添加默认值
     */
    @GetMapping("/list")
    public JsonResult listAndAdd(AccountConfigExt accountConfigExt) {
        startPage();
        List<AccountConfigExt> list = accountConfigExtService.selectAndAddAccountConfigExtList(accountConfigExt.getAccountId(), accountConfigExt.getParamKey());
        return JsonResult.ok(list);
    }

    /**
     * 挪车区域配置
     *
     * @param accountConfigExt
     * @return
     */
    @GetMapping("/getMoveConfig")
    public Result getMoveConfig(AccountConfigExt accountConfigExt) {
        MoveConfig moveConfig = accountConfigExtService.getMoveConfig(accountConfigExt);
        return Result.success(moveConfig);
    }

    /**
     * 新增挪车配置
     *
     * @param moveConfig
     * @return
     */
    @PostMapping("/addMoveConfig")
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.MOVE_CONFIG)
    public Result addMoveConfig(@RequestBody MoveConfig moveConfig) {
        if (moveConfig.getMoveDistance() == null || moveConfig.getSameParkPoint() == null || moveConfig.getClearLastBorrowTime() == null) {
            return Result.success();
        }
        accountConfigExtService.addMoveConfig(moveConfig);
        return Result.success();
    }

    /**
     * 新增区域参数扩展配置
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.SYSTEM_CONFIGS, methods = MethodEnum.ADD_ACCOUNT_CONFIG_EXT)
    public JsonResult add(@RequestBody String data) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        List<AccountConfigExt> accountConfigExts = JSONArray.parseArray(JSONObject.parseObject(data).getString("data"), AccountConfigExt.class);
        return JsonResult.toAjax(accountConfigExtService.insertAccountConfigExt(accountConfigExts), context);
    }


    /**
     * 检验是否配置优驱API和运营商三要素用量
     * @param brandId   品牌id
     * @return  Result<Boolean>
     */
    @GetMapping("/checkApiAndDosage")
    public Result<Map<String, Object>> checkApiAndDosage(Integer brandId){
        return Result.success(accountConfigExtService.checkApiAndDosage(brandId));
    }
}
